package com.my.common;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author zhaodongchao
 * @date 2020/3/14 21:03
 **/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result implements Serializable {
    private static final long serialVersionUID = 6727234023109899237L;

    private final static int SUCCESS_STATUS = 200;
    private final static int ERROR_STATUS = 500;
    private final static int TIMEOUT_STATUS = 0;
    private final static String SUCCESS_MESSAGE = "success";
    private final static String ERROR_MESSAGE = "error";


    private int status;
    private String message;
    private Object error;
    private Object data;
    private String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    private String path;

    public Result(int status, String message, Object error, Object data, String path) {
        this.status = status ;
        this.message = message ;
        this.error = error ;
        this.data = data ;
        this.path = path ;
    }


    public static Result ok(String message, Object data,  String path) {
        return new Result(SUCCESS_STATUS, message, null, data, path);
    }

    public static Result ok(String message, Object data) {
        return new Result(SUCCESS_STATUS, message,null, data, null);
    }

    public static Result ok(String message) {
        return new Result(SUCCESS_STATUS, message, null, null, null);
    }

    public static Result ok(Object data) {
        return new Result(SUCCESS_STATUS, SUCCESS_MESSAGE, null, data,  null);
    }
    public static Result ok() {
        return new Result(SUCCESS_STATUS, SUCCESS_MESSAGE, null,null, null);
    }

    public static Result error(String message ,Object error ,String path){
        return new Result(ERROR_STATUS, message, error, null, path);
    }
    public static Result error(String message ,Object error){
        return new Result(ERROR_STATUS, message, error, null,  null);
    }
    public static Result error(String message ){
        return new Result(ERROR_STATUS, message, null,  null, null);
    }

    public static Result error(int status , String message ){
        return new Result(status, message, null, null,  null);
    }

}
